package com.skyolder.crutch.ast;

import com.skyolder.crutch.error.GrammarError;
import com.skyolder.crutch.error.RunError;
import com.skyolder.crutch.ev.Environment;
import com.skyolder.crutch.word.TokenQueue;
import com.skyolder.crutch.word.token.IdentifierToken;
import com.skyolder.crutch.word.token.KeyWordToken;
import com.skyolder.crutch.word.token.SymbolToken;
import com.skyolder.crutch.word.token.Token;
import com.skyolder.crutch.word.types.KeyWordType;
import com.skyolder.crutch.word.types.SymbolType;

public class LocalVarTree implements Tree {
	Environment parentEnvironment = null;
	TokenQueue tokenQueue = null;
	String typeName=null;
	
	public LocalVarTree(Environment environment, TokenQueue tokenQueue){
		parentEnvironment=environment;
		this.tokenQueue=tokenQueue;
	}
	@Override
	public void build() throws GrammarError {
		// TODO Auto-generated method stub
		Token token=tokenQueue.nextToken();
		if(!(token instanceof KeyWordToken)){
			throw new GrammarError(token.toString());
		}
		if(((KeyWordToken)token).getKeyWordType()!=KeyWordType.VAR){
			throw new GrammarError(token.toString());
		}
		token=tokenQueue.nextToken();
		if(token instanceof KeyWordToken){
			KeyWordToken kToken=(KeyWordToken)token;
			if(!kToken.isVarType()){
				throw new GrammarError(kToken.toString());
			}
			typeName=kToken.getKeyWordType().toString();
		}else if(!(token instanceof IdentifierToken)){
			throw new GrammarError(token.toString());
		}else{
			typeName=((IdentifierToken)token).getIdent();
		}
		for(;;){
			token=tokenQueue.nextToken();
			if(!(token instanceof IdentifierToken)){
				throw new GrammarError(token.toString());
			}
			String varName=((IdentifierToken)token).getIdent();
			token=tokenQueue.nextToken();
			if(!(token instanceof SymbolToken)){
				throw new GrammarError(token.toString());
			}
			SymbolToken sToken=(SymbolToken)token;
			if(sToken.getSymbolType()==SymbolType.COMMA){
				continue;
			}else if(sToken.getSymbolType()==SymbolType.ASSIGN){
				
			}else if(sToken.getSymbolType()==SymbolType.SEMICOLON){
				return;
			}else{
				throw new GrammarError(token.toString());
			}
		}
	}

	@Override
	public void excute() throws RunError {
		// TODO Auto-generated method stub
		
	}
	public boolean probe() throws GrammarError{
		Token t=tokenQueue.peekToken();
		if(!(t instanceof KeyWordToken)){
			tokenQueue.back();
			return false;
		}
		if(((KeyWordToken)t).getKeyWordType()!=KeyWordType.VAR){
			tokenQueue.back();
			return false;
		}
		return true;
	}
}
